﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ExpertStockSystem.Models;
using ExpertStockSystem.DAL;
using System.Data.Entity;

namespace ExpertStockSystem
{
    public partial class MainWindow : Window
    {
        private WarehouseRepository warehouseRepository = new WarehouseRepository();
        public Product currentProduct;
        public MainWindow()
        {
            InitializeComponent();
            ReloadWarehouseLayout();
        }
        public void ReloadWarehouseLayout()
        {
            warehouseRepository = new WarehouseRepository();
            warehouseLayoutGrid.Children.Clear();
            System.Windows.Controls.Primitives.UniformGrid grid = new System.Windows.Controls.Primitives.UniformGrid();
            grid.Rows = warehouseRepository.MaxRow();
            grid.Columns = warehouseRepository.MaxColumn();

            for (int i = 1; i <= warehouseRepository.MaxRow(); i++)
            {
                for (int j = 1; j <= warehouseRepository.MaxColumn(); j++)
                {
                    if (warehouseRepository.IsWarehouseSpaceAvailable(i,j))
                    {
                        grid.Children.Add(new TextBlock());
                    }
                    else
                    {
                        WarehouseItemUserControl userControl = new WarehouseItemUserControl();
                        grid.Children.Add(userControl);
                    }
                }
            }
            

            warehouseLayoutGrid.Children.Add(grid);
        }

        private void exitButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void barcodeTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            barcodeTextBox.Clear();
        }

        public void AssignProduct(Product product)
        {
            try
            {
                currentProduct = product;
                productNameTextBlock.Text = currentProduct.Name;
                productNameTextBlock.Foreground = Brushes.Black;
                remainingTextBlock.Text = currentProduct.CurrentQuantity.ToString("n2");
                try
                {
                    stockTextBlock.Text = currentProduct.FirstAvailable.WarehouseSpace.Name +
                        " [#" + currentProduct.FirstAvailable.CurrentQuantity.ToString("n2") + " - 11/12/2010]";
                }
                catch
                {
                    stockTextBlock.Text = "0.00";
                }
                barcodeTextBlock.Text = currentProduct.Barcode;

                if (!(currentProduct.CurrentQuantity > 0))
                {
                    exportButton.IsEnabled = false;
                    remainingTextBlock.Foreground = Brushes.DarkRed;
                }
                else
                {
                    exportButton.IsEnabled = true;
                    remainingTextBlock.Foreground = Brushes.DarkGreen;
                }
                importButton.IsEnabled = true;

                barcodeTextBox.Clear();
            }
            catch { }
        }
        public void UnassignProduct()
        {
            if (currentProduct != null)
                currentProduct = null;
            productNameTextBlock.Text = "Scan barcode to show product details";
            productNameTextBlock.Foreground = Brushes.DarkGray;

            remainingTextBlock.Text = "";
            stockTextBlock.Text = "";

            importButton.IsEnabled = false;
            exportButton.IsEnabled = false;

            barcodeTextBlock.Text = "";
            barcodeTextBox.Focus();
        }

        private void barcodeTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {

            }

            if (e.Key == Key.Escape)
                UnassignProduct();
        }

    }
}
